<?php

/*
 * litchicms
 * Copyright (C) 2010-6-13 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(86);
(empty($CATEGORY_ID) || empty($COLLECTION_ENAME)) && exit("! category.PHP");

if (empty($_POST['action']))
    !empty($RT['v']['catac']) && $_POST['action'] = $RT['v']['catac'];

$can_action = array('from', 'update', 'add', 'add_post', 'edit', 'edit_post', 'move', 'move_post', 'del', 'clear');

$needTHISCATaction = array('add', 'add_post', 'edit', 'edit_post', 'move', 'move_post', 'del', 'clear');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action))
    $_POST['action'] = 'from';


require 'lr.class.php';
$cate = new category($CATEGORY_ID, $COLLECTION_ENAME);
$cate->loadsub();
$CATEGORY_ROOT = cat2expert($cate);
$ALL_cid = array();
foreach ($CATEGORY_ROOT as $value) {
    $ALL_cid[] = $value['cid'];
}


if (in_array($_POST['action'], $needTHISCATaction)) {
    if (empty($RT['v']['cid'])) {
        $_POST['action'] = 'from';
    } else {
        if (in_array($RT['v']['cid'], $ALL_cid)) {
            $THISCAT = new category($RT['v']['cid'], $COLLECTION_ENAME);
            $THIPATH = $THISCAT->getPath();
        } else {
            $_POST['action'] = 'from';
        }
    }
}

function unlinkceche() {
    if (is_file($file = SYSTEM . 'cache/category/' . $GLOBALS['CATEGORY_ID'] . '.php')) {
        unlink($file);
    }
}

switch ($_POST['action']) {
    case 'from':
//        dump($CATEGORY_ROOT);

        break;
    case 'update':

        foreach ($_POST['cat_name'] as $k => $v) {
            if (in_array($k, $ALL_cid)) {
                if (strlen($v) < 80) {
                    brk_update('category', array('cname' => $v), "WHERE cid=$k");
                }

                if (intval($_POST['cat_name'][$k]) < 65535) {
                    brk_update('category', array('csort' => $_POST['cat_sort'][$k]), "WHERE cid=$k");
                }
            }
        }
        unlinkceche();
        eMsg(1, "操作完成");
        $_POST['action'] = 'message';
        break;
    case 'add':
        $THISCAT = current(cat2expert($THISCAT));
        foreach ($THIPATH as &$value) {
            $value = current(cat2expert($value));
        }
        unset($value);
        array_unshift($BREADCRUMD, array('node' => base64($RT_M . $RT_S . $RT_A), 'note' => $THISCAT['cname']));
        array_unshift($BREADCRUMD, array('node' => "", 'note' => "添加子分类"));

        break;
    case 'add_post':
        if (empty($_POST['cat']['name']) || strlen($_POST['cat']['name']) > 240) {
            eMsg(0, "参数错误");
            $_POST['action'] = 'error';
            break;
        }
        $newcat = new category(0, $COLLECTION_ENAME);
        $newcat->setname($_POST['cat']['name']);
        $newcat->note = $_POST['cat']['note'];
        $newcat->url = $_POST['cat']['url'];
        $newcat->urlTarget = $_POST['cat']['urlTarget'];
        $newcat->tpl = $_POST['cat']['tpl'];
        $newcat->viewtpl = $_POST['cat']['viewtpl'];

        $THISCAT->add($newcat, TRUE);
        eMsg(1, "添加分类成功");
        $_POST['action'] = 'message';
        unlinkceche();
        break;
    case 'edit':
        $THISCAT = current(cat2expert($THISCAT));
        foreach ($THIPATH as &$value) {
            $value = current(cat2expert($value));
        }
//        dump($CATEGORY_ROOT);     
//        array_shift($CATEGORY_ROOT);
        unset($value);
        array_unshift($BREADCRUMD, array('node' => base64($RT_M . $RT_S . $RT_A), 'note' => $THISCAT['cname']));
        array_unshift($BREADCRUMD, array('node' => "", 'note' => "添加子分类"));

        break;
    case 'edit_post':
        $THISCAT = current(cat2expert($THISCAT));
        if (empty($_POST['cat']['name']) || strlen($_POST['cat']['name']) > 240) {
            eMsg(0, "参数错误");
            $_POST['action'] = 'error';
            break;
        }
        $dataarr = array(
            'cname' => $_POST['cat']['name'],
            'cnote' => $_POST['cat']['note'],
            'ctpl' => $_POST['cat']['tpl'],
            'cviewtpl' => $_POST['cat']['viewtpl'],
            'curl' => $_POST['cat']['url'],
            'curlTarget' => intval($_POST['cat']['urlTarget']),
        );
        brk_update('category', $dataarr, "WHERE cid=" . $THISCAT['cid']);
        eMsg(1, "编辑分类成功");
        $_POST['action'] = 'message';
        unlinkceche();
        break;
    case 'move':
        if ($THISCAT->id == $CATEGORY_ID) {
            eMsg(0, "不能移动根节点");
            $_POST['action'] = 'message';
            break;
        }
        $THISCAT->loadsub();
        $temp = cat2expert($THISCAT);
        $temp = array_merge($temp, cat2expert(current(array_slice($THIPATH, -2, 1))));
        $CATNOTinTREE = array();
        foreach ($temp as $value) {
            array_push($CATNOTinTREE, $value['cid']);
        }
        $THISCAT = current(cat2expert($THISCAT));
        break;
    case 'move_post':
        if ($THISCAT->id == $CATEGORY_ID) {
            eMsg(0, "不能移动根节点");
            $_POST['action'] = 'message';
            break;
        }
        $re = $THISCAT->move($_POST['cat']['parent'], $COLLECTION_ENAME, TRUE);
        if ($re !== TRUE) {
            eMsg(0, $re);
        } else {
            eMsg(1, "移动节点成功");
        }
        $_POST['action'] = 'message';
        unlinkceche();
        break;
    case 'del':
        if ($THISCAT->id == $CATEGORY_ID) {
            eMsg(0, "不能删除根节点");
            $_POST['action'] = 'message';
            break;
        }
        $THISCAT->destory(TRUE);
        eMsg(1, "操作完成");
        $_POST['action'] = 'message';
        unlinkceche();
        break;

    case 'clear':
        $THISCAT->clear(TRUE);
        eMsg(1, "操作完成");
        $_POST['action'] = 'message';
        unlinkceche();
        break;
    default:
        break;
}
?>